<template>
  <view>
    <view class="search_bg">
      <uni-search-bar :radius="100" @confirm="search" @input="input" cancelButton="none"></uni-search-bar>
    </view>
    <view class="sugg-list" v-if="searchList.length==0?false:true"> 
      <view class="sugg-item" v-for="(val,i) in searchList" :key="i" @click="goData(val)" >
        <view class="goods-name">{{val.goods_name}}</view>
        <uni-icons type="forward" size="17"></uni-icons>
       </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题 -->
        <view class="history-title">
         <text>搜索历史</text>
         <uni-icons type="trash" size="17" @click="clean"></uni-icons>

        </view>
        <!-- 历史内容 -->
        <view class="history-list" >
          <uni-tag :text="item" v-for="(item,i) in history" :key="i" @click="goList(item)"></uni-tag>
        </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        kw:'',
        searchList:[],
        historyList:[],
      };
    },
    methods: {
      input(e) {
          clearTimeout(this.timer)
          this.timer=setTimeout(() => {
          this.kw=e.value
          this.getList()
        }, 500)
      },
      onLoad(){
        // 数据一加载，获取储存的值渲染页面
        this.historyList=JSON.parse(uni.getStorageSync('kw')||'[]')
      },
      //获取搜索列表
      async getList(){
        if(this.kw.length===0){
          this.searchList=''
          return
        }
        let {data:res}=await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
        if(res.meta.status!==200)return uni.$showMsg()
        this.searchList=res.message
        this.saveSearchHistory(this.kw)
      },
      // 跳转页面
      goData(val){
        console.log(val);
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?query='+val.goods_id
        })
      },
      saveSearchHistory(kw){
        // this.historyList.push(kw)
        
        // 解决关键词重复
        // 定义set对象
        const set=new Set(this.historyList)
        set.delete(kw)
        set.add(kw)
        this.historyList=Array.from(set)
        
        // 将数据储存到本地
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      // 清空搜素历史操作
      clean(){
        this.historyList=[]
        uni.setStorageSync('kw','')
      },
      // 点击历史实现页面的跳转
      goList(item){
        uni.navigateTo({
          url:"/subpkg/goods_list/goods_list?query="+item
        })
      }
    },
    computed:{
      history(){
        return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
  .history-box {
    padding: 0 5px;
  
    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }
  
    .history-list {
      display: flex;
      flex-wrap: wrap;
  
      .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }

  .search_bg {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  .sugg-list {
    padding: 0 5px;
  
    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;
  
      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }

</style>
